//
//  MAHomeWorkEditFinishView.m
//  meiang_edu
//
//  Created by gong on 2018/9/13.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAHomeWorkEditFinishView.h"
#import <MapKit/MapKit.h>

@interface MAHomeWorkEditFinishView ()<UITextFieldDelegate,CLLocationManagerDelegate>

@property (strong,nonatomic) CLLocationManager* locationManager;

@end

@implementation MAHomeWorkEditFinishView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self makeUI];
    }
    return self;
}

-(void)makeUI{
    
    UIScrollView *scroll=[[UIScrollView alloc]initWithFrame:self.bounds];
    [self addSubview:scroll];
    scroll.backgroundColor=ViewBgColor;
    
    UIView *bgTopview = [[UIView alloc]initWithFrame:CGRectMake(4, 4, SCREEN_WIDTH-8, 50)];
    [scroll addSubview:bgTopview];
    bgTopview.backgroundColor=[UIColor whiteColor];
    bgTopview.layer.masksToBounds=YES;
    bgTopview.layer.cornerRadius=5;
    _bgTopview=bgTopview;
    
    
    _titL=[[UITextField alloc]initWithFrame:CGRectMake(6, 0, bgTopview.width-12, bgTopview.height)];
    _titL.font=[UIFont systemFontOfSize:FONT_SIZE_14];
    _titL.textColor=[UIColor colorWithHexString:@"666666"];
    _titL.placeholder=@"请在此输入标题";
    [bgTopview addSubview:_titL];
    _titL.delegate=self;
    
    
    _imgBG=[[UIView alloc]initWithFrame:CGRectMake(4, _bgTopview.bottom+4, _bgTopview.width, 68+12*2)];
    [scroll addSubview:_imgBG];
    _imgBG.backgroundColor=[UIColor whiteColor];
    _imgBG.layer.masksToBounds=YES;
    _imgBG.layer.cornerRadius =5;
    
    
    UIImageView *imgV=[[UIImageView alloc]initWithFrame:CGRectMake(12, 12, 108, 68)];
    [_imgBG addSubview:imgV];
    imgV.image=[UIImage imageNamed:@"addimage_logo"];
    _imgV=imgV;
    imgV.contentMode = UIViewContentModeScaleAspectFit;
    imgV.userInteractionEnabled=YES;
    UITapGestureRecognizer *gesImg=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgVClick)];
    [imgV addGestureRecognizer:gesImg];
    
    UIView *dingweiBg=[[UIView alloc]initWithFrame:CGRectMake(_bgTopview.x, _imgBG.bottom+4, _imgBG.width, _bgTopview.height)];
    [scroll addSubview:dingweiBg];
    dingweiBg.backgroundColor=[UIColor whiteColor];
    dingweiBg.layer.masksToBounds=YES;
    dingweiBg.layer.cornerRadius=5;
    
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(12, 0, 40, dingweiBg.height)];
    [dingweiBg addSubview:lab];
    lab.font=[UIFont systemFontOfSize:14];
    lab.text=@"定位";
    lab.textColor=[UIColor colorWithHexString:@"666666"];
    
    UILabel *addrL=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, dingweiBg.width-12, dingweiBg.height)];
    [dingweiBg addSubview:addrL];
    addrL.textColor=[UIColor colorWithHexString:@"999999"];
    addrL.font=[UIFont systemFontOfSize:14];
    addrL.textAlignment=NSTextAlignmentRight;
    addrL.text=@"定位请点击";
    
    _addressL=addrL;
    addrL.userInteractionEnabled=YES;
    UITapGestureRecognizer *ges=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addressClick)];
    [addrL addGestureRecognizer:ges];
    
    
        
    UIView *contentBgView = [[UIView alloc]initWithFrame:CGRectMake(4, _bgTopview.bottom+4, SCREEN_WIDTH-8, 50)];
    [scroll addSubview:contentBgView];
    contentBgView.backgroundColor=[UIColor whiteColor];
    contentBgView.layer.masksToBounds=YES;
    contentBgView.layer.cornerRadius=5;
    
    _contentF=[[UITextField alloc]initWithFrame:CGRectMake(6, 0, bgTopview.width-12, bgTopview.height)];
    _contentF.font=[UIFont systemFontOfSize:FONT_SIZE_14];
    _contentF.textColor=[UIColor colorWithHexString:@"666666"];
    _contentF.placeholder=@"请在此输入内容";
    [contentBgView addSubview:_contentF];
    
    _imgBG.frame = CGRectMake(4, contentBgView.bottom+4, _bgTopview.width, 68+12*2);
    dingweiBg.frame =CGRectMake(_bgTopview.x, _imgBG.bottom+4, _imgBG.width, _bgTopview.height);
    
    //
//    UIView *fenleiBgView = [[UIView alloc]initWithFrame:CGRectMake(4, dingweiBg.bottom+4, SCREEN_WIDTH-8, 50)];
//    [scroll addSubview:fenleiBgView];
//    fenleiBgView.backgroundColor=[UIColor whiteColor];
//    fenleiBgView.layer.masksToBounds=YES;
//    fenleiBgView.layer.cornerRadius=5;
//    
//    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(12, 0, 40, dingweiBg.height)];
//    [fenleiBgView addSubview:lab];
//    lab.font=[UIFont systemFontOfSize:14];
//    lab.text=@"分类";
//    lab.textColor=[UIColor colorWithHexString:@"666666"];
//    
//    
//    UILabel *addrL=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, dingweiBg.width-12, dingweiBg.height)];
//    [fenleiBgView addSubview:addrL];
//    addrL.textColor=[UIColor colorWithHexString:@"999999"];
//    addrL.font=[UIFont systemFontOfSize:14];
//    addrL.textAlignment=NSTextAlignmentRight;
//    addrL.text=@"请选择分类";
//    
//    _fenleiL=addrL;
//    addrL.userInteractionEnabled=YES;
//    UITapGestureRecognizer *ges=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fenleiClick)];
//    [addrL addGestureRecognizer:ges];
//    
//    
//    //
//    UIView *personBgView = [[UIView alloc]initWithFrame:CGRectMake(4, fenleiBgView.bottom+4, SCREEN_WIDTH-8, 50)];
//    [scroll addSubview:personBgView];
//    personBgView.backgroundColor=[UIColor whiteColor];
//    personBgView.layer.masksToBounds=YES;
//    personBgView.layer.cornerRadius=5;
//    
//    UILabel *perL=[[UILabel alloc]initWithFrame:CGRectMake(12, 0, 45, dingweiBg.height)];
//    [personBgView addSubview:perL];
//    perL.font=[UIFont systemFontOfSize:14];
//    perL.text=@"操作人";
//    perL.textColor=[UIColor colorWithHexString:@"666666"];
//    
//    
//    UILabel *personL=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, dingweiBg.width-12, dingweiBg.height)];
//    [personBgView addSubview:personL];
//    personL.textColor=[UIColor colorWithHexString:@"999999"];
//    personL.font=[UIFont systemFontOfSize:14];
//    personL.textAlignment=NSTextAlignmentRight;
//    personL.text=@"请选择操作人";
//    
//    _personL=personL;
//    personL.userInteractionEnabled=YES;
//    UITapGestureRecognizer *ges2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(personClick)];
//    [personL addGestureRecognizer:ges2];
    
    
}

-(void)addressClick{
    
    [self startLocation];
}


-(void)fenleiClick{
    
    if ([self.delegate respondsToSelector:@selector(categoryClick)]) {
        [self.delegate categoryClick];
    }
}

-(void)personClick{
    
    
}

-(void)imgVClick{
    if ([self.delegate respondsToSelector:@selector(imgDidClick)]) {
        [self.delegate imgDidClick];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField endEditing:YES];
    return YES;
}


//开始定位

-(void)startLocation{
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
            {
                [_locationManager requestWhenInUseAuthorization];
            }
            break;
        case kCLAuthorizationStatusDenied:
        {
            
        }
        default:
            break;
    }
    
}



- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {

    CLLocation *newLocation = locations[0];

    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;

    NSLog(@"旧的经度：%f,旧的纬度：%f",oldCoordinate.longitude,oldCoordinate.latitude);
    [manager stopUpdatingLocation];
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];

    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {
        
        for (CLPlacemark *place in placemarks) {
            
            NSLog(@"name,%@",place.name);                      // 位置名
            NSLog(@"thoroughfare,%@",place.thoroughfare);      // 街道
            NSLog(@"subThoroughfare,%@",place.subThoroughfare);// 子街道
            NSLog(@"locality,%@",place.locality);              // 市
            NSLog(@"subLocality,%@",place.subLocality);        // 区
            NSLog(@"country,%@",place.country);                // 国家
//            place.administrativeArea 省
            self.addressL.text = [NSString stringWithFormat:@"%@%@%@%@%@",place.country,place.administrativeArea,place.locality,place.subLocality,place.thoroughfare];
        }
    }];
}

-(void)getCategory{
 
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    [MBManager showLoading];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request) {
        request.requestSerializerType = ZBJSONRequestSerializer;
        
        request.URLString = [NSString stringWithFormat:@"%@%@?funcode=%@", GetCategory_url, @"CF97774C-D784-42AD-A48A-EFFBAC816F79",@"organization_zy_other"];
        request.methodType = ZBMethodTypeGET;
        [request setValue:userinfo.token forHeaderField:@"token"];
        request.apiType = ZBRequestTypeRefresh;
        
        NSLog(@"%@", request.URLString);
        
    } success:^(id responseObject, apiType type) {
        [MBManager hideAlert];
        // 解析数据
        NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",dataDict);
        NSInteger code = [dataDict[@"msgcode"] integerValue];
        if (code == 1) { // 请求成功
            [MBManager hideAlert];
            
            NSArray *valueArr = dataDict[@"value"];
            if (valueArr.count) {
                
                self.fenleiL.text = valueArr[0][@"corner_name"];
            }
            
        } else {
            [MBManager showBriefAlert:dataDict[@"message"]];
        }
    } failure:^(NSError *error) {
        DLog(@"%@",error);
        
    } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache) {
        if (isCache==YES) {
            DLog(@"使用了缓存");
        }
    }];
}

@end
